# Esta clase maneja los curriculums cargados para los Trainees. Permite buscar trainees a partir de tags en los mismos.
class CurriculumsController < ApplicationController
  
  before_filter :get_mod_cv, :only => [:new,:edit, :delete]
 
# Recupera todos los cv para ser mostrados en pantalla.
  def index
    @curriculums = Curriculum.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @curriculums }
    end
  end

  # Recupera el cv que va a ser mostrado en pantalla.
  def show
#  	@trainee = Trainee.find_by_id(params[:id])
    @curriculum = Curriculum.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @curriculum }
    end
  end

# Prepara el nuevo cv para su posterior creación efectiva.
  def new
    @curriculum = Curriculum.new
    @trainee = Trainee.find(params[:trainee_id])
    @trainee.curriculum = @curriculum
  
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @curriculum }
    end
  end

   # Recupera el cv que va a ser editado.
  def edit
    @curriculum = Curriculum.find(params[:id])
  end

# Crea el curriculum.
  def create
    @curriculum = Curriculum.new(params[:curriculum])
    @trainee.curriculum.build
   
    respond_to do |format|
      if @curriculum.save
      
        flash[:notice] = 'Curriculum was successfully created.'
        format.html { redirect_to(@curriculum) }
        format.xml  { render :xml => @curriculum, :status => :created, :location => @curriculum }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @curriculum.errors, :status => :unprocessable_entity }
      end
    end
  end


  # Actualiza el curriculum.
  def update
    @curriculum = Curriculum.find(params[:id])

    respond_to do |format|
      if @curriculum.update_attributes(params[:curriculum])
      
      	@trainee = @curriculum.trainee
        @log_trainee = LogTrainee.new
    		@log_trainee.fecha_ingreso = Date.today
    		@log_trainee.trainee = @trainee
    		@log_trainee.descripcion = 'Actualizacion de CV'
    		@log_trainee.save  
    		
        flash[:notice] = 'Curriculum was successfully updated.'
        format.html { redirect_to(@curriculum) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @curriculum.errors, :status => :unprocessable_entity }
      end
    end
  end

# Borrar efectivamente el curriculum.
  def destroy
    @curriculum = Curriculum.find(params[:id])
    @curriculum.destroy

    respond_to do |format|
      format.html { redirect_to(curriculums_url) }
      format.xml  { head :ok }
    end
  end
end
